package com.bnz.service.impl;

import com.bnz.dao.ClassesDao;
import com.bnz.dao.StudentDao;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentDao studentDao;
	@Autowired
	private ClassesDao classesDao;
	//1. 查询所有学生
	@Override
	public List<Student> findAll() {
		List<Student> students = studentDao.findAll();
		students.forEach(r->{
			 Classes classes = classesDao.findById(r.getCid()).get();
			 r.setCname(classes.getCname());
		 });
		 return students;
	}

	@Override
	public Student findById(int id) {
		Student student=studentDao.findById(id).get();

		return student;
	}

	@Override
	public PageResult<Student> findPage(Integer page, Integer pageSize) {

		Page<Student> studentPage = studentDao.findAll(PageRequest.of(page - 1, pageSize));

		for (Student student : studentPage) {
			Classes classes=classesDao.findById(student.getCid()).get();
			student.setCname(classes.getCname());
		}

		return new PageResult<>(studentPage.getTotalElements(),
				studentPage.getTotalPages(),
				page,
				studentPage.getContent());
	}

	@Override
	public PageResult<Student> search(Integer page, Integer pageSize, StudentVo vo) {
         //4.1 将查询条件封装到数组中
		List<Predicate> predicates = new ArrayList<>();
		Specification<Student> spec = new Specification<Student>() {
			//4.2 根据传入的参数将查询条件封装放到集合predicates中
			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if(vo != null){
					if(StringUtils.isNotBlank(vo.getSname())){
						Predicate sname = cb.like(root.get("sname").as(String.class), "%" + vo.getSname() + "%");
						predicates.add(sname);
					}
					if(StringUtils.isNotBlank(vo.getAddr())){
						Predicate addr = cb.like(root.get("addr").as(String.class), "%" + vo.getAddr() + "%");
						predicates.add(addr);
					}
					if(vo.getCid() != 0){
						Predicate cid = cb.equal(root.get("cid").as(int.class), vo.getCid());
						predicates.add(cid);
					}
				}
				//4.3 将条件集合转换为数组
				Predicate[] predicatesArr = predicates.toArray(new Predicate[predicates.size()]);
				//4.4 返回
				return cb.and(predicatesArr);
			}
		};
		//4.5 开始分页查询
		Page<Student> studentPage = studentDao.findAll(spec, PageRequest.of(page - 1, pageSize));
		//4.6 将学生与班级名称绑定
		for (Student student : studentPage.getContent()) {
			Classes classes = classesDao.findById(student.getCid()).get();
			student.setCname(classes.getCname());
		}
		//4.7 返回
		return new PageResult<>(studentPage.getTotalElements(),
				studentPage.getTotalPages(),
				page,
				studentPage.getContent());
	}

	@Override
	public void add(Student student) {
		studentDao.save(student);
	}

	@Override
	public void update(Student student) {
		studentDao.save(student);
	}

	@Override
	public void delete(String sid) {
		studentDao.deleteById(new Integer(sid));
	}

	@Override
	public List<Student> findByKeyword(String sname, String addr) {
		return studentDao.findBySnameLikeAndAddrLike(sname,addr);
	}

	//模糊查询
	@Override
	public List<Student> findAllss() {
		return studentDao.findStudents();
	}

	/*@Override
	public void addstudent(Student student) {
		studentDao.savess(student);
	}*/


}
